//
//  PhotoClipAreaLayer.swift
//  JJImagePicker
//
//  Created by 李加建 on 2021/6/1.
//

import UIKit

class PhotoClipAreaLayer: CALayer {
    var centerRect: CGRect
    init(frame: CGRect, centerRect: CGRect) {
        self.centerRect = centerRect
        super.init()
        self.frame = frame
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(in ctx: CGContext) {
        UIGraphicsPushContext(ctx)
        let pickingFieldPath = UIBezierPath(rect: centerRect)
        let bezierPathRect = UIBezierPath(rect: bounds)
        bezierPathRect.append(pickingFieldPath)
        UIColor.black.withAlphaComponent(0.6).set()
        bezierPathRect.usesEvenOddFillRule = true
        bezierPathRect.fill()
        UIColor.white.set()
        pickingFieldPath.lineWidth = 1
        pickingFieldPath.stroke()
        UIGraphicsPopContext()
        contentsGravity = CALayerContentsGravity.center
    }
}
